<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>定时器</title>
    <style>
      button {
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <button>停止定时器</button>

    <script>
      // 1、定时器的两种写法
      // 1.1 写法1
      setTimeout(function () {
        console.log('2秒时间到了！');
      }, 2000);

      // 1.2 写法2
      function callback() {
        console.log('3秒后爆炸');
      }
      setTimeout(callback, 3000);

      // 2、setInterval 的使用
      var timer3 = setInterval(function () {
        console.log('每隔4秒就执行');
      }, 4000);

      var btn = document.querySelector('button');
      btn.onclick = function () {
        clearInterval(timer3);
      };
    </script>
  </body>
</html>
